//
//  SubjectVC.m
//  Cat
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SubjectVC.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "XYZPhoto.h"
#import "ToolCoreData.h"

#import "FlyView.h"

#import "SubjectRightVC.h"


@interface SubjectVC ()
@property(nonatomic, strong)ALAssetsGroup *group;
@property(nonatomic, strong)NSMutableArray *imageArr;
@property(nonatomic, strong)XYZPhoto *photo;
@property (nonatomic, copy) NSString *ArrCount;
@end

@implementation SubjectVC



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    ToolCoreData *toolCoreData = [[ToolCoreData alloc] init];
    NSArray *picArr = [toolCoreData takePicture];
    
    if (picArr.count != self.imageArr.count) {
        
        for(UIView *view in [self.view subviews]) {
            [view removeFromSuperview];
        }
        [self movePhoto];
    }
    
}

- (void)movePhoto {
    ToolCoreData *toolCoreData = [[ToolCoreData alloc] init];
    NSArray *picArr = [toolCoreData takePicture];
    self.imageArr = [NSMutableArray arrayWithArray:picArr];
    
    for (NSInteger i = 0; i < picArr.count; i++) {
        float X = 0;
        float Y = ((arc4random()%100) + 64);
        float W = (arc4random() % (150 -100+1) + 100);
        float H = (arc4random() % (150 -100+1) + 100);
        
        if (i < 20) {
            self.photo = [[XYZPhoto alloc] initWithFrame:CGRectMake(X, Y, W, H)];
            [self.view addSubview:self.photo];
            [self.photo hahahImage:self.imageArr[i]];
            NSInteger j = arc4random() % (15);
            if (j > 10) {
                [self.photo setImageAlphaAndSpeedAndSize:(j - 3) * 0.1];
            } else {
                
                [self.photo setImageAlphaAndSpeedAndSize:(j + 2) * 0.1];
            }
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self movePhoto];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-4fangge (1).png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];

//    ALAssetsLibrary *assetsLibrary;
//    NSMutableArray *groupArray;
//    assetsLibrary = [[ALAssetsLibrary alloc] init];
//    groupArray = [[NSMutableArray alloc] initWithCapacity:1];
//    
//    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//        
//        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
//         
//            if (result) {
//                float X = 0;
//                float Y = ((arc4random()%100) + 64);
//                float W = (arc4random() % (150 -100+1) + 100);
//                float H = (arc4random() % (150 -100+1) + 100);
//                XYZPhoto *photo = [[XYZPhoto alloc] initWithFrame:CGRectMake(X, Y, W, H)];
//                [self.view addSubview:photo];
//                UIImage *image = [UIImage imageWithCGImage:result.thumbnail];
//                [photo updateImage:image];
//                [photo setImageAlphaAndSpeedAndSize:index * 2.0/ [group numberOfAssets]+0.2];
//                if (index == [group numberOfAssets] -1) {
//                    stop = false;
//                }
//            }
//        }];
//    } failureBlock:^(NSError *error) {
//        
//        
//    }];
    
  
}

#pragma mark - push
- (void)rightAction:(UIBarButtonItem *)button {
    SubjectRightVC *rightVC = [[SubjectRightVC alloc] init];
    [self.navigationController pushViewController:rightVC animated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
